package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.ExchangeGoldDiamondsDao;
import com.vf.admin.client.dao.ExchangeGoldDiamondsLogDao;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.ExchangeGoldDiamondsService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamonds;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamondsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ExchangeGoldDiamondsServiceImpl implements ExchangeGoldDiamondsService {

    @Autowired
    ExchangeGoldDiamondsDao exchangeGoldDiamondsDao;
    @Autowired
    AppUserService appUserService;
    @Autowired
    ExchangeGoldDiamondsLogDao exchangeGoldDiamondsLogDao;

    @Override
    public List<ExchangeGoldDiamonds> page(SplitPageBean splitPage, Map<String, Object> paraMap) {
        return exchangeGoldDiamondsDao.page(splitPage, paraMap);
    }

    @Override
    public List<ExchangeGoldDiamonds> list() {
        List<ExchangeGoldDiamonds> list = exchangeGoldDiamondsDao.list();
        for(ExchangeGoldDiamonds b : list) {
           b.setGoldCoinStr(b.getGoldCoin().stripTrailingZeros().toPlainString() + "金币");
           b.setDiamondsStr(b.getDiamonds().stripTrailingZeros().toPlainString() + "钻石");
        }
        return list;
    }

    @Override
    public ExchangeGoldDiamonds get(Integer id) {
        return exchangeGoldDiamondsDao.get(id);
    }

    @Override
    public void saveOrUpdate(ExchangeGoldDiamonds haoYou) {
        exchangeGoldDiamondsDao.saveOrUpdate(haoYou);
    }

    @Override
    public void del(Integer id) {
        exchangeGoldDiamondsDao.del(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result exchange(Integer userId, Integer id) {
        ExchangeGoldDiamonds exchangeGoldDiamonds = this.get(id);
        if(exchangeGoldDiamonds != null) {
            if(!appUserService.isEnoughGold(userId, exchangeGoldDiamonds.getGoldCoin())) {
                return Result.errorJSON("金币余额不足");
            }
            //扣金币
            appUserService.subJinbi(userId, exchangeGoldDiamonds.getGoldCoin(), "兑换钻石支出");
            //增加钻石
            appUserService.addDiamonds(userId, exchangeGoldDiamonds.getDiamonds(), "兑换钻石收入");
            //记录
            ExchangeGoldDiamondsLog log = new ExchangeGoldDiamondsLog();
            log.setUserId(userId);
            log.setImage(exchangeGoldDiamonds.getImage());
            log.setGoldCoin(exchangeGoldDiamonds.getGoldCoin());
            log.setDiamonds(exchangeGoldDiamonds.getDiamonds());
            log.setCreateTime(new Date());
            exchangeGoldDiamondsLogDao.add(log);
        }
        return Result.successJSON();
    }
}
